<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** Post Page **/
class Post extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model("posts");
		$this->load->model("categories");
		$this->load->helper('text');
	}
	
	public function index(){
		$value['title'] = "News";
		$value['news']	= $this->posts->getAllRecords(1,10);
		$value['info']	= $this->posts->getAllRecords(2,2);
		$value['latest']= $this->posts->getAllRecords(1,5);
		$value['head']  = $this->posts->getDataSingleRecord('',1);	
		$value['content'] = "vnews/vindex-news";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function read($year,$month,$day,$id){
		
		$value['title']	  = "News";	
		$value['post']	  = $this->posts->getDataSingleRecord($id,'',$year,$month,$day);
		$value['images']  = $this->posts->getImageByPost($id);
		$value['comment'] = $this->posts->getCommentByPost($id);	
		$value['latest']  = $this->posts->getAllRecords(1,5,'now');
		$value['latest2'] = $this->posts->getAllRecords(2,5);
		$value['meta']	  = $value['post']['post_title'];	
		$value['content'] = "vnews/vread-news";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function add(){
		$value['title']	  = "News";
		$value['cat']	  = $this->categories->getDropdownCategories();
		$value['latest']  = $this->posts->getAllRecords(1,5);
		$value['latest2'] = $this->posts->getAllRecords(2,5);
		$value['content'] = "vnews/vadd";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function uploadImage(){
		$config['upload_path'] = './assets/post/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	   = '0';
		$config['max_width']   = '0';
		$config['max_height']  = '0';
		$config['file_name']   = date('Y-m-d').'-'.seoUrl($this->input->post('title'));
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('image')):
			$data = '';
			//redirect('err');
		else:
			$data = $this->upload->data();
		endif;
		
		if(!empty($data))
			$image = $data['file_name'];
		else
			$image = '';
		return $image;
	}
	
	public function uploadExtraImage(){
		for($i=1;$i<=10;$i++):
			$config['upload_path'] = './assets/post/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	   = '0';
			$config['max_width']   = '0';
			$config['max_height']  = '0';
			$config['file_name']   = date('Y-m-d').'-'.seoUrl($this->input->post('title').'-'.$i);
			$this->load->library('upload', $config);
			$image = 'image'.$i;
			if (!$this->upload->do_upload($image)):
				$data = '';
			else:
				$data = $this->upload->data();
			endif;
			
			if(!empty($data)):
				$image = $data['file_name'];
				$this->posts->saveImage($image);
			else:
				$image = '';
			endif;	
			//return $image;
		endfor;	
	}
	
	public function save(){
		$image = $this->uploadImage(); // call image
		$this->uploadExtraImage();
		$this->posts->savePost($image);
		$this->session->set_flashdata('message','<h5 style="color:red">Save Successfully for Title '.$this->input->post('title').'</h5>');
		redirect('home',301);
	}
	
	public function saveComment(){
		$this->posts->saveComment();
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function edit($id){
		$value['title']	  = "News";
		$value['cat']	  = $this->categories->getDropdownCategories();
		$value['post']	  = $this->posts->getDataSingleRecord($id);
		$value['latest']  = $this->posts->getAllRecords(1,5,'now');
		$value['latest2'] = $this->posts->getAllRecords(2,5,'now');
		$value['content'] = "vnews/vedit";
		$this->load->view('themes/bdo/index',$value);
	}
	
	public function update(){
		$image = $this->uploadImage(); // call image
		$this->posts->updatePost($image);
		redirect('post/edit/'.$this->input->post('id'),301);
	}
	
	public function remove($id=''){
		if($_POST){
			$id = $this->input->post('id');
		}
		$this->posts->removePost($id);
		$this->posts->removeCommentByPost($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function removeComment($id){
		if($_POST){
			$id = $this->input->post('id');
		}
		
		$this->posts->removeComment($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
}